﻿/*  
	------------------------------------------------------------------------
	 NQ Core Library
	
	 Homepage: http://www.awzhome.de/
	------------------------------------------------------------------------
	
	This Source Code Form is subject to the terms of the Mozilla Public
	License, v. 2.0. If a copy of the MPL was not distributed with this
	file, You can obtain one at http://mozilla.org/MPL/2.0/.
   
	The Original Code is code of NQ Core Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2012 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	------------------------------------------------------------------------
*/

using System;

namespace AWZhome.NQ.Core.Fluent
{

	/// <summary>
	/// Wraps a service instance to be used with the NQ fluent interface.
	/// </summary>
	/// <typeparam name="T">Type of the service instance.</typeparam>
	public class ServiceInstanceWrapper<T>
	{
		protected T _serviceObject = default(T);
		protected LogWrapper<T> _logWrapper;

		internal ServiceInstanceWrapper(T serviceObject)
			: this()
		{
			_serviceObject = serviceObject;
		}

		protected ServiceInstanceWrapper()
		{
			_logWrapper = new LogWrapper<T>(this);
		}

		/// <summary>
		/// Returns the wrapped service instance.
		/// </summary>
		//[Obsolete("Please use Instance property instead.")]
		public virtual T To
		{
			get
			{
				return _serviceObject;
			}
		}

		/// <summary>
		/// Returns the wrapped service instance.
		/// </summary>
		public virtual T Instance
		{
			get
			{
				return _serviceObject;
			}
		}

		/// <summary>
		/// Returns an <see cref="AWZhome.NQ.Core.INQServiceInfo">INQServiceInfo</see>
		/// instance describing the service the object belongs to.
		/// </summary>
		public virtual INQServiceInfo MetaInfo
		{
			get
			{
				return NQServiceManager.Instance.GetServiceInfoFromObject(_serviceObject);
			}
		}

		/// <summary>
		/// Returns a log wrapper to access the log functionality
		/// of NQ Service Manager.
		/// </summary>
		public virtual LogWrapper<T> Log
		{
			get
			{
				return _logWrapper.From(_serviceObject);
			}
		}

		/// <summary>
		/// Invokes (calls the invoke method of) the wrapped service instance.
		/// </summary>
		/// <returns>The instance of the wrapper.</returns>
		public virtual ServiceInstanceWrapper<T> Invoke()
		{
			NQServiceManager.Instance.InvokeService(_serviceObject);
			return this;
		}

		/// <summary>
		/// Quits (calls the quit method of) the wrapped service instance.
		/// </summary>
		/// <returns>The instance of the wrapper.</returns>
		public virtual ServiceInstanceWrapper<T> Quit()
		{
			NQServiceManager.Instance.QuitService(_serviceObject);
			return this;
		}
	}
}
